﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.WPF
{
    public class WindowsHelper
    {
        /// <summary>
        /// 在注册表注册开机自动运行
        /// </summary>
        /// <param name="key"></param>
        /// <param name="exePath"></param>
        public static void AutoRunWidthRegistry(string key, string exePath)
        {
            RegistryKey rk = Registry.CurrentUser;//当前用户的启动项。如果设为LocalMachine（所有用户）则需要管理员权限
            RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            rk2.SetValue(key, $"\"{exePath}\"");
            rk2.Close();
            rk.Close();
        }
        /// <summary>
        /// 取消注册表里的开机自动运行
        /// </summary>
        /// <param name="key"></param>
        /// <param name="exePath"></param>
        public static void AbortAutoRunWidthRegistry(string key, string exePath)
        {
            RegistryKey rk = Registry.CurrentUser;
            RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            var value = rk2.GetValue(key);
            if (value is string val && val.ToLower() == exePath.ToLower())
            {
                rk2.DeleteValue(key, false);
            }

            rk2.Close();
            rk.Close();

        }
        /// <summary>
        /// 获取程序是否在注册表里注册了开机自动运行
        /// </summary>
        /// <param name="key"></param>
        /// <param name="exePath"></param>
        /// <returns></returns>
        public static bool IsAutoRunWidthRegistry(string key, string exePath)
        {
            RegistryKey rk = Registry.CurrentUser;
            RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            var value = rk2.GetValue(key);
            bool isautorun = false;
            if (value is string val && val.ToLower() == exePath.ToLower())
            {
                isautorun = true;
            }

            rk2.Close();
            rk.Close();
            return isautorun;
        }
        /// <summary>
        /// 使用CMD打开对象
        /// </summary>
        /// <param name="path"></param>
        public static void OpenUseCmd(string path)
        {
            string target = path;
            //target = target.Replace("^", "^^");
            //target = target.Replace(">", "^>");
            //target = target.Replace("<", "^<");
            //target = target.Replace("=", "^=");
            //target = target.Replace("|", "^|");
            //target = target.Replace("'", "^'");
            //target = target.Replace("`", "^`");
            //target = target.Replace(",", "^,");
            //target = target.Replace(";", "^;");
            //target = target.Replace("(", "^(");
            //target = target.Replace(")", "^)");
            //target = target.Replace("\"", "\\\""); 
            target = target.Replace("&", "^&"); 
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            //向cmd窗口发送输入信息 后面的&exit告诉cmd运行好之后就退出
            p.StandardInput.WriteLine("start " + target + "&exit");
            p.StandardInput.AutoFlush = true;
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
        }
    }
}
